package cn.wuxing.mediation.framework.web.service;


import cn.wuxing.mediation.system.service.ISysUserService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Component
public class DingTalkAuthenticationProvider implements AuthenticationProvider {

    private static final String ACCESS_TOKEN_URL = "https://oapi.dingtalk.com/gettoken";
    private static final String APP_KEY = "wxznmtyydy_dingoa-MQn8oNR7juQj";
    private static final String APP_SECRET = "4OIwZh04Kt92c1054sI5Eh20Y8Ye4gn6a1AGfBU0";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysPermissionService permissionService;



    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        String code = (String) authentication.getCredentials();
        if (StringUtil.isNotEmpty(code) ) {


        } else {

        }
        return null; // 不是扫码登录请求，不处理
    }


    @Override
    public boolean supports(Class<?> authentication) {
        return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
    }



    public String getAccessToken() {
        RestTemplate restTemplate = new RestTemplate();
        String url = UriComponentsBuilder.fromHttpUrl(ACCESS_TOKEN_URL)
                .queryParam("appkey", APP_KEY)
                .queryParam("appsecret", APP_SECRET)
                .toUriString();

        // 请求获取 access_token
        String response = restTemplate.getForObject(url, String.class);
        // 解析返回的 JSON，获取 access_token
        // 假设返回中包含 "access_token" 字段
        // 这里需要根据实际返回内容解析出 access_token
        return "token"; // 实现解析逻辑
    }
}
